package com.lionel.singleton;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * @author 714936489@qq.com
 * @date 2018/5/17 23:02
 */
@Slf4j
public class SingletonExample6 {
    private static AtomicInteger count = new AtomicInteger(0);

    public static SingletonExample6 getInstance() {
        return Singleton.SINGLETON.getInstance();
    }

    private enum Singleton {
        SINGLETON;
        private SingletonExample6 singleton;

        Singleton() {
            log.info("私有构造函数被实例化：{}次", count.incrementAndGet());
            singleton = new SingletonExample6();
            log.info("私有构造函数实例化完成...");
        }

        private SingletonExample6 getInstance() {
            return singleton;
        }
    }
}
@Slf4j
class SingletonExample6Tester{
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        for (int i = 0;i<20;i++) {
            executorService.execute(() -> {
                log.info("ThreadName:{},Object:{}",
                        Thread.currentThread().getName(), SingletonExample6.getInstance());
            });
        }
        executorService.shutdown();
    }
}
